function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
    });
    return UUID(uuid);
};


var L1 = db.CriteriaLevel1.findOne({ Name: 'ESG Performance' });
var crL1Id = L1._id;

var crL2Id = generateUUID();
// Add GC Level 2
if (!db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })) {
    
    db.CriteriaLevel2.insert({ _id: crL2Id, CriteriaLevel1Id: crL1Id, UniverseFeature: 8, Name: 'ESG Controversies', ShortName: 'EsgControversies' });
}


if (!db.TemplateCriteria.findOne({ CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id })) {
    db.TemplateCriteria.insert([
            {
                _id: generateUUID(),
                Name: "Business Ethics Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "G.1.5",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Customer Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "S.3.3",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Employee Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "S.1.7",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Environmental Supply Chain Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "E.2.2",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Governance Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "G.2.13",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Operations Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "E.1.12",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Product & Service Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "E.3.2",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Public Policy Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "G.3.4",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Social Supply Chain Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "S.2.3",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
            {
                _id: generateUUID(),
                Name: "Society & Community Incidents",
                CriteriaEntity: "RPIndicator",
                CriteriaEntityField: "SelectedAnswer.Score",
                Score: null,
                TemplateIndicatorNumber: "S.4.3",
                CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: 'ESG Controversies' })._id,
                CriteriaType: 8,
                FilterId: null,
                CompaniesIdsResult: null
            },
        ],
        { ordered: true }
    );
}
